//
//  JBWMoveMenuView1.m
//  JBWDSK
//
//  Created by 崔志伟 on 2018/7/27.
//  Copyright © 2018年 崔志伟. All rights reserved.
//

#import "JBWMoveMenuView.h"
#import "UIButton+JBWTitleImgEdgeInsets.h"

@interface JBWMoveMenuView ()
{
    UIButton *btns[4];
}
@property (nonatomic,strong) UIButton *menuBtn ;


@end

@implementation JBWMoveMenuView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self initilize];
        [self setupSubView];
    }
    return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initilize];
        
    }
    return self;
}
-(void)initilize{
    self.safeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 25;
    self.backgroundColor =[UIColor clearColor];
    //添加手势
    UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(doMoveAction:)];
    [self addGestureRecognizer:panGestureRecognizer];
}

- (void)setupSubView{
    
    self.menuBtn = [[UIButton alloc]initWithFrame:CGRectMake(5,5 , 40, 40)];
    [self.menuBtn setImage:GetImageNamed(@"sdk_ic_log_all") forState:UIControlStateNormal];
    [self.menuBtn setImage:GetImageNamed(@"ic_close_style2") forState:UIControlStateSelected];
    self.menuBtn.layer.masksToBounds = YES;
    self.menuBtn.layer.cornerRadius = 20;
    [self.menuBtn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.menuBtn];
    
    NSArray *btnImgs = @[@"ic_me_pre",@"ic_gift_pre",@"ic_kefu_pre",@"ic_news_pre"];
    NSArray *btnTitles = @[@"我的",@"礼包",@"客服",@"资讯"];

    for (int i = 0; i< 4; i++) {
        UIButton *itemBtn =   [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
        [itemBtn setImage:GetImageNamed(btnImgs[i]) forState:UIControlStateNormal];
        [itemBtn setTitle:btnTitles[i] forState:UIControlStateNormal];
        itemBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        itemBtn.tag = 3000+i;
        [itemBtn addTarget:self action:@selector(itemBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [itemBtn layoutButtonTitleImageEdgeInsetsWithStyle:(JBWButtonEdgeInsetsStyleBottom) titleImgSpace:5];
        btns[i] = itemBtn;
        itemBtn.hidden = YES;
        [self addSubview:itemBtn];
    }

}


-(void)itemBtnAction:(UIButton *)sender{
        if (self.selectItemBlcok) {
            self.hidden = YES;
            self.selectItemBlcok(sender.tag -3000);
        }
    
    
}

- (void)btnAction{
    self.menuBtn.selected = !self.menuBtn.selected;
    if (self.menuBtn.selected) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
        [UIView animateWithDuration:0.2 animations:^{
            if (self.center.x < JBW_SCREEN_WIDTH/2 ) {
                self.frame = CGRectMake(0, self.frame.origin.y-5, 240, 60);
                self.menuBtn.frame =CGRectMake(5,10 , 40, 40);
                for (int i = 0; i< 4; i++) {
                    self->btns[i].hidden = NO;
                    self->btns[i].center = CGPointMake(50+20+40*i, 30);
                }
            }else{
                self.frame = CGRectMake(JBW_SCREEN_WIDTH-240, self.frame.origin.y-5, 240, 60);
                for (int i = 0; i< 4; i++) {
                    self->btns[i].hidden = NO;
                    self->btns[i].center = CGPointMake(240-40-35-40*i, 30);
                }

                self.menuBtn.frame =CGRectMake(240-5-40,10 , 40, 40);
            }
        }];
       
    }else{
        self.backgroundColor = [UIColor clearColor];
        if (self.center.x < JBW_SCREEN_WIDTH/2 ) {
            self.frame = CGRectMake(0, self.frame.origin.y+5, 50, 50);
        }else{
            self.frame = CGRectMake(JBW_SCREEN_WIDTH-50, self.frame.origin.y+5, 50, 50);
        }
        for (int i = 0; i< 4; i++) {
            self->btns[i].hidden = YES;
            self->btns[i].center = CGPointMake(25, 30);
        }
        self.menuBtn.frame =CGRectMake(5,5 , 40, 40);
    }
}


-(void)setParentView:(UIView *)parentView{
    _parentView = parentView;
}
#pragma mark - 手势方法
- (void)doMoveAction:(UIPanGestureRecognizer *)recognizer
{
    //1、手势在self.view坐标系中移动的位置
    CGPoint translation = [recognizer translationInView:self.parentView];
    CGPoint newCenter = CGPointMake(recognizer.view.center.x + translation.x,
                                    recognizer.view.center.y + translation.y);
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.5 animations:^{
            //设置中心点范围
            if (newCenter.x < JBW_SCREEN_WIDTH/2 ) {
             recognizer.view.center = CGPointMake(recognizer.view.frame.size.width/2, newCenter.y);
            }else{
            recognizer.view.center = CGPointMake(JBW_SCREEN_WIDTH -(recognizer.view.frame.size.width/2), newCenter.y);
            }
        }];
    }else{
        
        //2、限制屏幕范围：
        //上边界的限制
        newCenter.y = MAX(recognizer.view.frame.size.height/2 + self.safeInsets.top, newCenter.y);
        
        //下边界的限制
        newCenter.y = MIN(JBW_SCREEN_HEIGHT - self.safeInsets.bottom- recognizer.view.frame.size.height/2, newCenter.y);
        
        //左边界的限制
        newCenter.x = MAX(recognizer.view.frame.size.width/2, newCenter.x);
        
        //右边界的限制
        newCenter.x = MIN(JBW_SCREEN_WIDTH - recognizer.view.frame.size.width/2,newCenter.x);
        
        //设置中心点范围
        recognizer.view.center = newCenter;
        //3、将手势坐标点归0、否则会累加
        [recognizer setTranslation:CGPointZero inView:self.parentView];
    }
}

@end

